/**
* <p>Title: EleOperationUtil.java</p>
* <p>Description: 鍏冪礌鎿嶄綔utils</p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2015骞�1鏈�7鏃�
* @version 1.0
*/
package com.hxbd.test.utils.webdriver;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import com.duan.test.utils.common.JdbcUtils;
/**
 * G7Sweb绯荤粺 鍏冪礌鎿嶄綔銆佷笌鍏冪礌鐩稿叧銆丟7S鐩稿叧鏁版嵁搴撹繛鎺ョ瓑鍏叡鏂规硶灏佽绫�
 */
public class EleOperationUtil extends FindElementUtil{
	
	/**
	 * 鐐瑰嚮
	 * @param by By
	 * @param driver WebDriver
	 */
	public static void click (By by, WebDriver driver) {
		waitElementClickable(driver, by).click();
	}
	/**
	 * 鐐瑰嚮
	 * @param we WebElement
	 * @param driver WebDriver
	 */
	public static void click (WebElement we, WebDriver driver) {
		waitElementClickable(driver,we).click();
	}
//	/**
//	 * 鍙戦�佹枃鏈�
//	 * @param by By
//	 * @param driver WebDriver
//	 * @param str
//	 */
//	public static void sendkeys (By by, WebDriver driver, String str) {
//		waitElementAppear(driver, by).sendKeys(str);
//	}
//	/**
//	 * 鏍规嵁text閫夋嫨涓嬫媺鍒楄〃椤�
//	 * @param by By
//	 * @param driver WebDriver
//	 * @param text 鍒楄〃椤瑰悕
//	 */
//	public static void selectItemForInnerText (By by, WebDriver driver, String text) {
//		new Select(waitElementAppear(driver, by)).selectByVisibleText(text);
//	}
	/**
	 * 鏍规嵁text閫夋嫨涓嬫媺鍒楄〃椤�
	 * @param we webelement
	 * @param text 鍒楄〃椤瑰悕
	 */
	public static void selectItemForInnerText (WebDriver driver, WebElement we, String text) {
		waitElementDisPlay(driver, we);
		new Select(we).selectByVisibleText(text);
	}
	/**
	 * 鏍规嵁index閫夋嫨涓嬫媺鍒楄〃椤�
	 * @param we webelement
	 * @param index 鍒楄〃椤逛綅缃�
	 */
	public static void selectItemForIndex (WebDriver driver, WebElement we, int index) {
		waitElementDisPlay(driver, we);
		new Select(we).selectByIndex(index);
	}
	/**
	 * 鏍规嵁value閫夋嫨涓嬫媺鍒楄〃椤�
	 * @param we webelement
	 * @param value 鍒楄〃椤瑰湪html鐨剉alue鍊�
	 */
	public static void selectItemForValue (WebDriver driver,WebElement we, String value) {
		waitElementDisPlay(driver, we);
		new Select(we).selectByValue(value);
	}
//	/**
//	 * 鏍规嵁index閫夋嫨涓嬫媺鍒楄〃椤�
//	 * @param by By
//	 * @param driver WebDriver
//	 * @param index 鍒楄〃椤逛綅缃�
//	 */
//	public static void selectItemForIndex (By by, WebDriver driver, int index) {
//		new Select(waitElementAppear(driver, by)).selectByIndex(index);
//	}
//	/**
//	 * 鏍规嵁value閫夋嫨涓嬫媺鍒楄〃椤�
//	 * @param by By
//	 * @param driver WebDriver
//	 * @param value 鍒楄〃椤瑰湪html鐨剉alue鍊�
//	 */
//	public static void selectItemForValue (By by, WebDriver driver, String value) {
//		new Select(waitElementAppear(driver, by)).selectByValue(value);
//	}
	/**
	 * 宸﹀彸鍒囨崲妗嗛�夋嫨鍏冪礌
	 * @param itemsStr 澶氫釜鍏冪礌缁勬垚鐨勫瓧绗︿覆
	 * @param select 閫夋嫨鎸夐挳
	 * @param add 娣诲姞鎸夐挳
	 * @param save 淇濆瓨鎸夐挳
	 */
	public static void selectCheckList (WebDriver driver, String itemsStr,WebElement select, WebElement add, WebElement save) {
		String []items = itemsStr.split(",");
		for (int i = 0; i < items.length; i++) {
			try {
				selectItemForInnerText(driver,select, items[i]);
			} catch (NoSuchElementException e) {
				e.printStackTrace();
				Assert.fail("鍒楄〃鎵句笉鍒�" + items[i]);
			}
			add.click();
		}
		save.click();
	}
	
	/**
	 * 鏍规嵁琛屻�佸垪鍧愭爣鑾峰彇table涓厓绱犳枃鏈�
	 * @param driver webdriver
	 * @param rownum 鎵�鍦ㄨ
	 * @param colnum 鎵�鍦ㄥ垪
	 * @return string鍏冪礌鏂囨湰
	 */
	public static String getInnerTextOfTableEle (WebDriver driver, int rownum, int colnum) {
		return waitElementAppear(driver, By.xpath("//tbody/tr[" + rownum + "]/td[" + colnum + "]")).getText();
	}

	/**
	 * 绛夊緟js鍔犺浇瀹屾垚
	 * @param driver
	 * @return Boolean:
	 * 			true:鍔犺浇瀹�
	 * 			fasle:鏈姞杞藉畬
	 */
	public static Boolean waitForJsResult(WebDriver driver)
    {
		WebDriverWait wait = new WebDriverWait(driver, 30);
  	  	Boolean flag = wait.until
  			  (new ExpectedCondition<Boolean>() {
  	            public Boolean apply(WebDriver d) {
  	            	JavascriptExecutor jse = (JavascriptExecutor)d;
  	            	return (Boolean)jse.executeScript("return jQuery.active == 0");
  	            }
  	        });
  	  	return flag;
    }
	
	/**
	 * 绛夊緟鎼滅储鏃舵暟鎹姞杞藉畬鎴�
	 * @param driver
	 * @param we
	 * @param text
	 * @return Boolean
	 */
	public static Boolean waitForJsResult(WebDriver driver,WebElement we,String text)
    {
		WebDriverWait wait = new WebDriverWait(driver, 30);
		Boolean flag;
		try {
			flag = wait.until
	  			  (ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(we, text)));
		}catch (TimeoutException e) {
			flag = false;
		}
		return flag;
    }
	/**
	 * 绛夊緟鎼滅储鏁版嵁鍔犺浇瀹屾垚
	 * @param driver
	 * @param we
	 * @return Boolean
	 */
	public static Boolean waitForJsResult(WebDriver driver,WebElement we)
    {
		return waitForJsResult(driver, we, "鎼滅储涓�");
    }

	/**
	 * 鑾峰彇table涓暟鎹畁um
	 * @param tb_info 椤甸潰鏄剧ず鍏冪礌鏁伴噺鐨勫厓绱�
	 * @return int
	 */
	public static int getTableEleNum (WebElement tb_info) {
		int datanum;
		String dataStr = tb_info.getText();
		if (dataStr.trim().equalsIgnoreCase("鏃犳暟鎹�")) {
			datanum = 0;
		}
		else {
			String []searchResult = dataStr.split("/");
			String str = searchResult[1].replace(" ", "").replace(",","");		
			String temp = str.substring(1, str.length()-3);
			datanum = Integer.parseInt(temp);
		}
		return datanum;
	}
	/**
	 * 鑾峰彇褰撳墠椤甸潰鏁版嵁澶у皬
	 * @param tb_info 椤甸潰鏄剧ず鍏冪礌鏁伴噺鐨勫厓绱�
	 * @return int
	 */
	public static int getPageSize (WebElement  tb_info) {
		String []searchResult = tb_info.getText().split("/");
		String tempstr = searchResult[0].replace(" ", "");
		int tempindex = tempstr.indexOf("鍒�");
		String temp2 = tempstr.substring(tempindex + 1, tempstr.length());
		return Integer.parseInt(temp2);
	}

	public static void selectOneOptionForText (WebElement we, String text) {
		Select clsSelect = null;
		try{            
            clsSelect = new Select(we);
	    }catch(StaleElementReferenceException e){
	    	clsSelect = new Select(we);
	    }
        clsSelect.selectByVisibleText(text);
	}
	/**
	 * 鑾峰彇MAP涓璿alue瀵瑰簲閿��
	 * @param map Map
	 * @param Mapvalue Map涓璙alue鍊�
	 * @return value鍊�
	 */
	public static Object getMapKey (Map<?, ?> map, Object Mapvalue) {
		Set<?> keys = map.entrySet(); 
		Object key = null;
		if(keys != null) {  			  
			for (Iterator<?> iterator = keys.iterator( ); iterator.hasNext();) {
				Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
				if (entry.getValue().equals(Mapvalue)) {
					key = entry.getKey();
					break;
				}
			}
		}
		return key;
	}
	
	/**
	 * 姣旇緝涓や釜瀛楃涓插鐞�(鍘诲皬鏁扮偣鏈熬0)鍚庡悗鏄惁鐩哥瓑
	 * @param str1
	 * @param str2
	 * @return Boolean
	 */
	public Boolean ownEquals(String str1,String str2) {
		Boolean temp = false;
		if (str1.equals("--")) {
			str1 = "";
		}
		if (str2.equals("--")) {
			str2 = "";
		}
		if (subZeroAndDot(str1).equals(subZeroAndDot(str2))) {
			temp = true;
		}
		return temp;
	}
	/**
	 * 鍘婚櫎灏忔暟鐐瑰悗闈㈢殑0锛屽鏋滃彇鍑哄悗鏈熬涓�.杩�.涓�璧峰幓闄�
	 * @param s 寰呭鐞嗗瓧绗︿覆
	 * @return String
	 */
	public static String subZeroAndDot(String s){  
        if(s.indexOf(".") > 0){  
            s = s.replaceAll("0+?$", "");//鍘绘帀澶氫綑鐨�0  
            s = s.replaceAll("[.]$", "");//濡傛渶鍚庝竴浣嶆槸.鍒欏幓鎺�  
        }  
        return s;  
    }  
	
	/**
	 * 椤甸潰婊氬姩鏉℃粴鍔�
	 * @param driver
	 * @param height 婊氬姩楂樺害
	 */
	public static void setScroll(WebDriver driver,int height){  
        try {  
            String setscroll = "document.documentElement.scrollTop=" + height;  
              
            JavascriptExecutor jse=(JavascriptExecutor) driver;  
            jse.executeScript(setscroll);  
        } catch (Exception e) {  
            System.out.println("Fail to set the scroll.");  
        }             
	}  
	/**
	 * 婊氬姩鍒扮洰鏍囧厓绱犵殑绾靛潗鏍囦綅缃�(Link)
	 * @param driver WebDriver
	 * @param we WebElement
	 */
	public void scrollAndClick(WebDriver driver, WebElement we)
	{
	   int elementPosition = we.getLocation().getY();
	   String js = String.format("window.scroll(0, %s)", elementPosition);
	   ((JavascriptExecutor)driver).executeScript(js);
	   we.click();
	}
	
	/**
	 * 鏍规嵁text閫夋嫨鍗曢�夋寜閽�
	 * @param radios 寰呴�夋墍鏈塺adio
	 * @param text 鏄剧ず鏂囨湰
	 */
	public void selectRadioForText (List<WebElement> radios, String text) {
		for (WebElement radio : radios) {
			if (radio.getText().trim().equals(text)) {
				if (!radio.isSelected()) {
					radio.click();
					Assert.assertTrue(radio.isSelected());
					break;
				}
			}
		}
	}
	/**
	 * 鏍规嵁value閫夋嫨鍗曢�夋寜閽�
	 * @param radios 寰呴�夋墍鏈塺adio
	 * @param value value鍊�
	 */
	public void selectRadioForIndex (List<WebElement> radios, String value) {
		for (WebElement radio : radios) {
			if (radio.getText().trim().equals(value)) {
				if (!radio.isSelected()) {
					radio.click();
					Assert.assertTrue(radio.isSelected());
					break;
				}
			}
		}
	}
	
	/**
	 * 鏍规嵁浣嶇疆閫夋嫨鍗曢�夋寜閽�
	 * @param radios 寰呴�夋墍鏈塺adio
	 * @param index 浣嶇疆
	 */
	public void selectRadioForIndex (List<WebElement> radios, int index) {
		WebElement wantRadio = radios.get(index + 1);
		if (!wantRadio.isSelected()) {
			wantRadio.click();
			Assert.assertTrue(wantRadio.isSelected());
		}
	}	
}
